package com.yanqu.road.server.manager.activity.musicroomrank;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.musicroomrank.MusicRoomRankConfig;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrossMusicRoomRankConfigMgr extends TempCrossMgr {
    private static Map<Integer, MusicRoomRankConfig> configMap = new HashMap<>();

    @Override
    public boolean removeExpireData() throws Exception {
        return false;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        reloadActivityConfig();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static synchronized void reloadActivityConfig() {
        getLogger().info("reload CrossMusicRoomRankConfigMgr start");
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.MusicRoomRank.getValue());

        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        for (ActivityInfo item : activityInfoMap.values()) {
            reloadConfig(item);
        }
        getLogger().info("reload CrossMusicRoomRankConfigMgr end");
    }

    public static synchronized MusicRoomRankConfig reloadConfig(ActivityInfo activityInfo) {
        MusicRoomRankConfig config = new MusicRoomRankConfig(activityInfo);
        configMap.put(config.getActivityInfo().getActivityId(), config);
        getLogger().info("reload CrossMusicRoomRankConfig success ,activityId{}",activityInfo.getActivityId());
        return config;
    }

    public static MusicRoomRankConfig getConfig(int activityId) {
        return configMap.get(activityId);
    }

    public static void removeConfig(int activityId) {
        configMap.remove(activityId);
    }
}
